import { ReactViewState, ReactView, reactControl, ReactViewControl } from "pao-aop-client";
import SignFrameLayout, { InputType } from "src/business/components/buss-components/sign-frame-layout";
import React from "react";
import { addon, Permission } from "pao-aop";
import { AppServiceUtility } from "src/projects/app/appService";
import { Modal, message } from "antd";
import { InputType as ListInputType } from "src/business/components/buss-components/sign-frame-layout";
import { ModalSearch } from "src/business/components/buss-components/modal-search";
import { ROUTE_PATH } from "src/projects/router";
/**
 * 组件：设备列表状态
 */
export interface DeviceViewState extends ReactViewState {
    /** 对话框 */
    visible?: boolean;
    /** 人员id */
    user_id?: string;
    /** 设备id */
    device_id?: string;
    /** 设备编号 */
    imei?: string;
    /** 接口名 */
    request_url?: string;
}

/**
 * 组件：设备列表
 * 描述
 */
export class DeviceView extends ReactView<DeviceViewControl, DeviceViewState> {
    private columns_data_source = [{
        title: '设备名称',
        dataIndex: 'name',
        key: 'name',
    }, {
        title: '设备编号',
        dataIndex: 'imei',
        key: 'imei',
    }, {
        title: '设备型号ID',
        dataIndex: 'model_id',
        key: 'model_id',
    }, {
        title: '设备软件ID',
        dataIndex: 'software_id',
        key: 'software_id',
    }, {
        title: '位置',
        dataIndex: 'pos',
        key: 'pos',
    }, {
        title: '所属人',
        dataIndex: 'user_name',
        key: 'user_name',
    }, {
        title: '备注',
        dataIndex: 'remarks',
        key: 'remarks',
    }];
    private columns_data_source_per = [{
        title: '姓名',
        dataIndex: 'name',
        key: 'name',
    }, {
        title: '身份证号码',
        dataIndex: 'id_card',
        key: 'id_card',
    }, {
        title: '地址',
        dataIndex: 'address',
        key: 'address',
    }, {
        title: '联系电话',
        dataIndex: 'telephone',
        key: 'telephone',
    },];
    constructor(props: DeviceViewControl) {
        super(props);
        this.state = {
            visible: false,
            user_id: '',
            device_id: '',
            imei: '',
            request_url: '',
        };
    }
    componentWillMount() {
        let request_url = this.props.request_url;
        this.setState({
            request_url
            // condition: [{ personnel_category: personnelCategory.elder }, 1, 10] // 长者资料
        });
    }
    /** 自定义图标按钮回调事件 */
    onIconClick = (type: string, contents: any) => {
        if ('icon_edit' === type) {
            console.log('自定义按钮edit返回值：', contents);
            this.setState({
                visible: true,
                device_id: contents.id,
                imei: contents.imei,
            });
        } else if ('icon_snippets' === type) {
            console.info(contents.id);
            this.props.history!.push(ROUTE_PATH.DeviceLog + '/' + contents.id);
        }
    }
    handleOk = () => {
        AppServiceUtility.device_service.update_device!({ 'id': this.state.device_id, 'user_id': this.state.user_id })!
            .then((data) => {
                if (data) {
                    this.setState({
                        visible: false,
                    });
                    message.info('绑定成功');

                } else {
                    message.info('绑定失败');
                }
            })
            .catch(err => {
                console.info(err);
            });
    }
    handleCancel = () => {
        this.setState({
            visible: false,
            device_id: '',
            user_id: '',

        });
    }
    changeDdvice = (value: any) => {
        this.setState({
            user_id: value
        });
    }
    render() {
        let service_item = {
            type_show: false,
            edit_form_items_props: [{
                type: InputType.input,
                label: "设备名称",
                decorator_id: "name",
                option: {
                    placeholder: "请输入设备名称",
                }
            }, {
                type: InputType.input,
                label: "设备编号",
                decorator_id: "imei",
                option: {
                    placeholder: "请输入设备编号",
                }
            }],
            columns_data_source: this.columns_data_source,
            on_icon_click: this.onIconClick,
            service_object: AppServiceUtility.device_service,
            service_option: {
                select: {
                    service_func: this.state.request_url,
                    service_condition: []
                },
                delete: {
                    service_func: 'delete_device'
                }
            },
            permission_class: this.props.permission_class,
            get_permission_name: this.props.get_permission_name,
            select_permission: this.props.edit_permission,
            edit_permission: this.props.edit_permission,
            add_permission: this.props.add_permission,
            delete_permission: this.props.delete_permission,
        };
        let modal_search_items_props = {
            edit_form_items_props: [
                {
                    type: ListInputType.input,
                    label: "选择人员",
                    decorator_id: "name"
                },
            ],
            columns_data_source: this.columns_data_source_per,
            service_name: AppServiceUtility.personnel_service,
            service_func: 'get_personnel_list_all',
            title: '人员查询',
            select_option: {
                placeholder: "请选择人员",
            }
        };
        let table_params = {
            other_label_type: [{ type: 'icon', label_key: 'icon_edit', label_parameter: { icon: 'antd@edit' } }, { type: 'icon', label_key: 'icon_snippets', label_parameter: { icon: 'antd@snippets' } }],
            showHeader: true,
            bordered: false,
            show_footer: true,
            rowKey: 'id',
        };
        let service_item_list = Object.assign(service_item, table_params);
        return (
            <div>
                <SignFrameLayout {...service_item_list} />
                <Modal
                    title="绑定设备"
                    visible={this.state.visible}
                    onOk={this.handleOk}
                    onCancel={this.handleCancel}
                >
                    <div style={{ marginBottom: '20px' }}>
                        设备编号:{this.state.imei}
                    </div>
                    <ModalSearch modal_search_items_props={modal_search_items_props} onChange={this.changeDdvice} />
                </Modal>
            </div>
        );
    }
}

/**
 * 设备列表页面
 */
@addon('DeviceView', '设备列表页面', '描述')
@reactControl(DeviceView, true)
export class DeviceViewControl extends ReactViewControl {
    /** 视图权限 */
    public permission?: Permission;
    /** 请求接口名 */
    public request_url?: string;
    /** 获取权限接口类 */
    public permission_class?: any;
    /** 获取权限接口方法名 */
    public get_permission_name?: string;
    /** 查询权限 */
    public select_permission?: string;
    /** 编辑权限 */
    public edit_permission?: string;
    /** 删除权限 */
    public delete_permission?: string;
    /** 新增权限 */
    public add_permission?: string;
} 